package com.bicon.botu.mall.goods.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bicon.botu.mall.goods.commons.GoodsConstants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.goods.entity.ClassAttributeItem;
import com.bicon.botu.mall.goods.service.IClassAttributeItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 分类属性项
 * @author songxu
 * @since 2017-07-19
 */
@RestController
@RequestMapping("/V0.0.1/classAttributeItem")
public class ClassAttributeItemController extends BaseController{

    @Autowired
    private IClassAttributeItemService iClassAttributeItemService;  //商品规格项Service

    /**
     * 商品规格项新增
     * @param classAttributeItem
     * @return
     */
    @RequestMapping(value="save",method = RequestMethod.POST)
    public BaseResult save(ClassAttributeItem classAttributeItem){
        if(classAttributeItem!=null){
            //商品分类属性ID、分类属性值、标识、排序 参数校验
            if(StringUtils.isNotBlank(classAttributeItem.getAttributeId()) && StringUtils.isNotBlank(classAttributeItem.getAttributeValue())
                    && StringUtils.isNotBlank(classAttributeItem.getAttributeMark())
                    && classAttributeItem.getRank()!=null){
                //初始化ID、创建时间、修改时间
                classAttributeItem.preInsert();
                //商品规格保存或修改 操作数据库
                boolean result=iClassAttributeItemService.insertOrUpdate(classAttributeItem);
                if(result){
                    return sendResult(GoodsConstants.RESPONSE_CODE_200,GoodsConstants.RESPONSE_MESSAGE_200);
                }else{
                    return sendResult(GoodsConstants.RESPONSE_CODE_600,GoodsConstants.RESPONSE_MESSAGE_600);
                }
            }else{
                return sendResult(GoodsConstants.RESPONSE_CODE_400,GoodsConstants.RESPONSE_MESSAGE_400);
            }
        }else{
            return sendResult(GoodsConstants.RESPONSE_CODE_400,GoodsConstants.RESPONSE_MESSAGE_400);
        }
    }

    /**
     * 商品规格项 修改
     * @param classAttributeItem
     * @return
     */
    @RequestMapping(value="modify",method = RequestMethod.POST)
    public BaseResult modify(ClassAttributeItem classAttributeItem){
        if(classAttributeItem!=null){
            //商品分类属性ID、分类属性值、标识、排序 参数校验
            if(StringUtils.isNotBlank(classAttributeItem.getId())){
                //设置修改时间
                classAttributeItem.setUpdateDate(new Date());
                //商品规格保存或修改 操作数据库
                boolean result=iClassAttributeItemService.insertOrUpdate(classAttributeItem);
                if(result){
                    return sendResult(GoodsConstants.RESPONSE_CODE_200,GoodsConstants.RESPONSE_MESSAGE_200);
                }else{
                    return sendResult(GoodsConstants.RESPONSE_CODE_600,GoodsConstants.RESPONSE_MESSAGE_600);
                }
            }else{
                return sendResult(GoodsConstants.RESPONSE_CODE_200,GoodsConstants.RESPONSE_MESSAGE_200);
            }
        }else{
            return sendResult(GoodsConstants.RESPONSE_CODE_400,GoodsConstants.RESPONSE_MESSAGE_400);
        }
    }

    /**
     * 商品规格项删除
     * @param id
     * @return
     */
    @RequestMapping(value="delete/{id}",method = RequestMethod.GET)
    public BaseResult delete(@PathVariable("id") String id){
        boolean result=false;
        result=iClassAttributeItemService.deleteById(id);
        if(result){
            return sendResult(GoodsConstants.RESPONSE_CODE_200,GoodsConstants.RESPONSE_MESSAGE_200);
        }else{
            return sendResult(GoodsConstants.RESPONSE_CODE_600,GoodsConstants.RESPONSE_MESSAGE_600);
        }
    }

    /**
     *  根据商品规格ID查询规格项集合
     * @param attributeId
     * @return
     */
    @RequestMapping(value="findByAttributeId/{attributeId}/{attributeMark}",method = RequestMethod.GET)
    public BaseResult findByAttributeId(@PathVariable("attributeId") String attributeId,@PathVariable("attributeMark") String attributeMark){
        //组装查询条件
        EntityWrapper<ClassAttributeItem> entityWrapper=new EntityWrapper<>();
        entityWrapper.eq("attribute_id",attributeId);
        entityWrapper.eq("attribute_mark",attributeMark);
        entityWrapper.orderBy("rank",true);
        List<ClassAttributeItem> classAttributeItemList=iClassAttributeItemService.selectList(entityWrapper);
        return sendResult(GoodsConstants.RESPONSE_CODE_200,GoodsConstants.RESPONSE_MESSAGE_200,classAttributeItemList);
    }

    /**
     * 批量排序接口
     * @param rankJson
     * @return
     */
    @RequestMapping(value = "batchUpdateRank",method = RequestMethod.POST)
    public BaseResult batchUpdateRank(String rankJson){
        //校验参数JSON是否为空
        if(StringUtils.isNotBlank(rankJson)){
            //JSON 转规格项集合
            List<ClassAttributeItem> classAttributeItems= JSONArray.parseArray(rankJson,ClassAttributeItem.class);
            if(classAttributeItems!=null && classAttributeItems.size()>0){
                //批量操作修改数据排序字段
                boolean result=iClassAttributeItemService.updateBatchById(classAttributeItems);
                if(result){
                    return sendResult(GoodsConstants.RESPONSE_CODE_200,GoodsConstants.RESPONSE_MESSAGE_200);
                }else{
                    return sendResult(GoodsConstants.RESPONSE_CODE_600,GoodsConstants.RESPONSE_MESSAGE_600);
                }
            }else {
                return sendResult(GoodsConstants.RESPONSE_CODE_400,GoodsConstants.RESPONSE_MESSAGE_400);
            }
        }else{
            return sendResult(GoodsConstants.RESPONSE_CODE_400,GoodsConstants.RESPONSE_MESSAGE_400);
        }
    }

}
